﻿/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2022-02-24 11:10
 * 描  述：对象类型
 */
var refreshGirdData;
var ProjectId = parent.request('ProjectId');
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGird();
            page.bind();

        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                page.search();
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                var UpObjectTypeID = $('#gridtable').jfGridValue('ObjectTypeID');
                learun.layerForm({
                    id: 'form',
                    title: '新增',
                    url: top.$.rootUrl + '/ZZDT_EC/ec_objecttype/Form?UpObjectTypeID=' + UpObjectTypeID + "&ProjectId=" + ProjectId,
                    width: 650,
                    height: 360,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                if (learun.checkrow(keyValue)) {
                    if (keyValue == "OPC") {
                        learun.alert.warning("OPC为系统内置的特殊类型，无法编辑其属性！");
                    }
                    else {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑',
                            url: top.$.rootUrl + '/ZZDT_EC/ec_objecttype/Form?keyValue=' + keyValue + "&ProjectId=" + ProjectId,
                            //后端还是form这个action接收到了，然后？后面的参数，在form.js中，会通过request函数去解析里面的内容
                            //request函数定义在login/index中 

                            width: 650,
                            height: 550,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }

                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                if (learun.checkrow(keyValue)) {
                    if (keyValue == "OPC") {
                        learun.alert.warning("OPC为系统内置的特殊类型，无法删除！");
                        return false;
                    }
                    //先判断是否在工程数据表已存在数据  不存在就删除，存在就提示信息
                    $.ajax({
                        type: 'GET',
                        url: top.$.rootUrl + "/ZZDT_EC/ec_objecttype/IsHaveEngineData?rdo=" + Math.random(),
                        data: { ProjectId: ProjectId, objectTypeID: keyValue },
                        traditional: true,
                        success: function (data) {
                            var data = JSON.parse(data);
                            if (data.code == 200) {
                                if (data.data) {
                                    learun.alert.error('该对象类型及子类型中包含设计数据，不能被删除！');
                                } else {
                                    learun.layerConfirm('是否确认删除该项！', function (res) {
                                        if (res) {
                                            learun.deleteForm(top.$.rootUrl + '/ZZDT_EC/ec_objecttype/DeleteForm', { keyValue: keyValue, ProjectId: ProjectId }, function () {
                                                refreshGirdData();
                                            });
                                        }
                                    });
                                }
                            }
                        },
                        error: function (msg) {
                            learun.alert.error(msg);
                        }, complete: function (data) {
                        }
                    });

                }
            });
            // 属性设置
            $('#lr_propertyset').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                if (learun.checkrow(keyValue)) {
                    var isParent = $('#gridtable').jfGridValue('IsParent');
                    if (isParent != "0") {
                        learun.alert.warning('请选择最后一级！');
                        return false;
                    }
                    if (keyValue == "OPC") {
                        learun.alert.warning("OPC为系统内置的特殊类型 \u000d 无法进行“属性设置”！");
                        return false;
                    }
                    learun.layerForm({
                        id: 'form',
                        title: '属性设置',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_property/SelectProperty?ObjectTypeID=' + keyValue + "&ProjectId=" + ProjectId,
                        width: 850,
                        height: 600,
                        btn: null
                    });
                }
            });
            //命名规则
            $('#lr_namingconv').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                if (learun.checkrow(keyValue)) {
                    var isParent = $('#gridtable').jfGridValue('IsParent');//IsParent 是在后端sql查询语句里用case when做的一个列名
                    if (isParent != "0") {
                        learun.alert.warning('请选择最后一级！');
                        return false;
                    }
                    if (keyValue == "OPC") {
                        learun.alert.warning("OPC为系统内置的特殊类型 \u000d 无法进行“属性设置”！");
                        return false;
                    }
                    learun.layerForm({
                        id: 'formnamingconv',//窗口id
                        title: '命名规则',//标题
                        url: top.$.rootUrl + '/ZZDT_EC/ec_objecttypenamingconv/Index?ObjectTypeID=' + keyValue + "&ProjectId=" + ProjectId,//窗体打开页面连接
                        width: 800,
                        height: 500,
                        maxmin: true,
                        btn: null//窗体底部显示按钮
                    });
                }
            });
            //选择元件库
            $('#lr_selectlibrary').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ObjectTypeID');
                if (learun.checkrow(keyValue)) {
                    var isParent = $('#gridtable').jfGridValue('IsParent');
                    if (isParent != "0") {
                        learun.alert.warning('请选择最后一级！');
                        return false;
                    }
                    learun.layerForm({
                        id: 'selectlibrary',
                        title: '选择元件库',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_library_file/SelectIndex?ObjectTypeID=' + keyValue + "&ProjectId=" + ProjectId,
                        width: 850,
                        height: 600,
                        btn: null
                    });
                }
            });
        },
        initGird: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_objecttype/GetList',
                headData: [
                    { label: '对象类型名称', name: 'ObjectTypeName', width: 500, align: "left", sort: false },
                    { label: '对象类型名称(英文)', name: 'ObjectTypeNameEN', width: 300, align: "left", sort: false },
                    {
                        label: "是否生成流水号", name: "IsSerialNumber", width: 120, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            if (cellvalue == '1') {
                                return "是";
                            } else {
                                return "否";
                            }
                        }
                    },
                    { label: '排序', name: 'OrderID', width: 100, align: "center", sort: false },
                    { name: "", width: 1, sort: false }
                ],
                mainId: 'ObjectTypeID',
                isTree: true,
                parentId: 'UpObjectTypeID',
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.KeyWordAndDir = $("#ObjectTypeName").val();
            param.ProjectId = ProjectId;
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    page.init();
}
